package com.developmentReformProject.controller;

import com.developmentReformProject.utils.FileUtil;
import com.developmentReformProject.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Controller
@RequestMapping("/api/file")
public class FileController {
    /**
     * 文件上传
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @ResponseBody
    public ResultVo upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        //文件上传
        String fileName= FileUtil.upload(file,request);
        /*
         * 响应
         * 将上传之后的文件名以"fileName"为key封装到data,
         */
        Map<String,String> data=new HashMap<>();
        data.put("fileName", fileName);
        ResultVo resultVo=new ResultVo(data);
        return resultVo;
    }
    /**
     * 文件下载
     * @param filename 传入下载的文件名
     * @param resp
     * @throws Exception
     */
    @GetMapping("/download")
    public void download(String filename, HttpServletResponse resp) throws Exception {
        FileUtil.download(filename, resp);
    }


}

